### General options for the compiler ###
CXX = nvcc
CXXFLAGS = --compiler-options "-g -W -Wall -Wshadow -Wcast-qual -Werror"
NVCC = nvcc
NVCCFLAGS = -G --compiler-options "-g -W -Wall"
LDFLAGS = $(CXXFLAGS)
LIBRARIES =

### Directories paths ###
BIN_DIR = bin
INC_DIR = include
LIB_DIR = 
OBJ_DIR = obj
SRC_DIR = src

### Virtual paths parameters ###
vpath %.h $(INC_DIR)
vpath %.cpp $(SRC_DIR)
vpath %.cu $(SRC_DIR)
.LIBPATTERNS = lib%.so lib%.dylib lib%.a
vpath lib%.a $(LIB_DIR)

### Set of files ###
SOURCES = $(patsubst $(SRC_DIR)/%, %, $(wildcard $(SRC_DIR)/*.cpp))
CUDA_SOURCES = $(patsubst $(SRC_DIR)/%, %, $(wildcard $(SRC_DIR)/*.cu))
HEADERS = $(patsubst $(INC_DIR)/%, %, $(wildcard $(INC_DIR)/*.h))
OBJECTS = $(patsubst %.cpp, $(OBJ_DIR)/%.o, $(SOURCES))
CUDA_OBJECTS = $(patsubst %.cu, $(OBJ_DIR)/%.o, $(CUDA_SOURCES))
DEPENDENCIES_FILE = makefile_dependencies

### Options for the target executable ###
TARGET = canalSAT


### Compilation of the target executable ###
all : $(BIN_DIR)/$(TARGET)

$(BIN_DIR)/$(TARGET) : $(OBJECTS) $(CUDA_OBJECTS)
	$(CXX) $(LDFLAGS) $^ -o $@


### Compilation of the objects ###
$(OBJECTS) : $(OBJ_DIR)/%.o : %.cpp
	$(CXX) $(CXXFLAGS) $(foreach X, $(INC_DIR), -I$(X)) -c $< -o $@

$(CUDA_OBJECTS) : $(OBJ_DIR)/%.o : %.cu
	$(NVCC) $(NVCCFLAGS) $(foreach X, $(INC_DIR), -I$(X)) -c $< -o $@

include $(DEPENDENCIES_FILE)


### Automatic generation of the dependencies file ###
$(DEPENDENCIES_FILE) : $(SOURCES) $(CUDA_SOURCES) $(HEADERS)
	@echo "Regenerating the dependencies...\n"
	@-makedepend -f- -s"### Dependencies ###" \
		     $(foreach X, $(INC_DIR), -I$(X)) \
		     $(addprefix $(SRC_DIR)/, $(SOURCES) $(CUDA_SOURCES)) \
		     -Y > tmp 2> /dev/null && mv tmp $(DEPENDENCIES_FILE)
	@sed s/$(SRC_DIR)/'$$(OBJ_DIR)'/ $(DEPENDENCIES_FILE) > tmp
	@mv tmp $(DEPENDENCIES_FILE)
	@sed s/' include\/'/' '/g $(DEPENDENCIES_FILE) > tmp
	@mv tmp $(DEPENDENCIES_FILE)


### Command to launch the program within memory-checking programs ###
valgrind :
	valgrind --dsymutil=yes --leak-check=full --show-reachable=yes \
		 --track-fds=yes --suppressions="CUDA.supp" \
		 $(BIN_DIR)/$(TARGET) $(DATA_FILE) $N

cuda-memcheck :
	cuda-memcheck --leak-check full $(BIN_DIR)/$(TARGET) $(DATA_FILE) $N


### Cleaning command ###
clean :
	rm -f $(OBJECTS) $(CUDA_OBJECTS)
mrproper : clean
	rm -f $(BIN_DIR)/*
